package cn.sanli.manage.utils;

import cn.sanli.manage.pojo.entity.Corres;
import cn.sanli.manage.pojo.entity.Organization;

import java.util.ArrayList;
import java.util.List;

/**
 * ClassName:OrganizationUtils
 * Package:cn.sanli.manage.utils
 * Description:
 *
 * @Author Lu
 * @Create 2023/12/16 16:48
 * @Version 1.0
 */
public class OrganizationUtils {
    public static List<Organization> buildTree(List<Organization> sysMenuList) {
        //新建一个用于接收数据的list
        List<Organization> trees = new ArrayList<>();
        for (Organization sysMenu : sysMenuList) {
            if (sysMenu.getParentId().longValue() == 1) {   //查询菜单入口 找到顶层数据 parentId==1
                //传递第一层菜单数据和所有菜单数据
                trees.add(findChildren(sysMenu,sysMenuList));
            }
        }
        return trees;
    }

    /**
     * 递归查找子节点
     * @param treeNodes
     * @return
     */
    public static Organization findChildren(Organization sysMenu, List<Organization> treeNodes) {
        sysMenu.setChildren(new ArrayList<Organization>());

        for (Organization it : treeNodes) {
            if(sysMenu.getId().longValue() == it.getParentId().longValue()) {
                if (sysMenu.getChildren() == null) {
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(findChildren(it,treeNodes));
            }
        }
        return sysMenu;
    }
}
